package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.RoleCaseMemberService;
import com.yixun.qfbao.api.service.RoleResourcesService;
import com.yixun.qfbao.apidto.AuthorizeRoleReqDto;
import com.yixun.qfbao.apidto.AuthorizeRoleResDto;
import com.yixun.qfbao.apidto.SetRoleCaseMemberReqDto;
import com.yixun.qfbao.apidto.SetRoleCaseMemberResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.AuthorizeRoleReqVo;
import com.yixun.qfbao.vo.SetRoleCaseMemberReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("setRoleCasememberHander")
public class SetRoleCasememberHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(SetRoleCasememberHander.class);

    @Autowired
    private RoleCaseMemberService roleCaseMemberService;
    @Autowired
    private LoginUtils loginUtils;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        BaseResponseDTO<SetRoleCaseMemberResDto> res = new BaseResponseDTO<>();
        SetRoleCaseMemberResDto resDto = new SetRoleCaseMemberResDto();
        SetRoleCaseMemberReqVo reqVo = (SetRoleCaseMemberReqVo) reqDto;
        SetRoleCaseMemberReqDto reqDto1 = reqVo.getParams();
        PartnerInfo pi = loginUtils.getUser(reqDto1.getUserId());
        if (pi == null) {
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("未找到用户信息");
            return res;
        }
        try {
            reqDto1.setMerchantId(pi.getHigherid());
            roleCaseMemberService.saveRoleCaseMember(reqDto1);
            resDto.setResult(ResultEnum.SUCCESS.getNo());
        } catch (Exception e) {
            logger.error("setRoleCasememberHander error: {}", e);
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("角色机构权限授权失败");
        }
        res.setResult(resDto.getResult());
        res.setResData(resDto);
        return res;
    }
}
